/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const BaseJsonAdapter = require('../base/BaseJsonAdapter');
const iconv = require("iconv-lite");
const vm = require('vm');
const request = require('request-promise');
const logger = require('../../conf/logger');
const querystring = require("querystring")
const urlencode = require("urlencode")

/**
 * 北京互金网
 * @type {module.Bjp2p}
 */
module.exports = class CnJinRong extends BaseJsonAdapter{

    async doRequest(){
        var form = {
            pageNum: 1,
            pageSize: 20,
            jsonStr: "{\"typeid\":\"14\"}"
        }

        var formData = querystring.stringify(form);
        var contentLength = formData.length;

        var options = {
            headers:{
                'Content-Length': contentLength,
                'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'
            },
            // uri:'http://www.bjp2p.com.cn/supervise/queryNews4List',
            uri:this.requestUrl,
            body:formData,
            method:"POSt",
            encoding:null
        }
        let body = await request(options)
        return body;
    }


    async reqJson(){
		const body = await this.doRequest();
        let decodeBody = iconv.decode(body, this.encoding).toString();
        decodeBody = urlencode.decode(decodeBody,this.encoding);
        decodeBody = decodeBody.replace(/\\/g,'');
        decodeBody = decodeBody.substr(1,decodeBody.length-2);
        let data = JSON.parse(decodeBody);
        console.log("-----decodeBody: " + decodeBody)
		return data;
	}
}
